<?php
	
class SearchController extends Zend_Controller_Action
{
	protected $apiKey = "0E8S7VSHS30YDDCG2JR2";
	protected $secretKey = "ziK6hJ+PfcAL1zaMgatJF7hq9KDChnuJ5zF/SXEl";
	protected $countryCode = "US";
	
	public function preDispatch()
	{
		if(!Zend_Auth::getInstance()->hasIdentity())
		{
			$this->_redirect('/login/index');
		}
	}
	
    public function init()
    {
        /* Initialize action controller here */
    }

	function test_print($item2)
	{
    	echo "$item2<br />\n";
	}
    public function indexAction()
    {
    	$search = new Application_Form_SearchForm(array(
            'action' => '/search/index',
            'method' => 'post',
        ));
        $this->view->form = $search;
        if($this->getRequest()->isPost())
        {
        	if($search->isValid($_POST)){
        		
        		//This block of code separates the ISBNs based on '\r\n' or ','
        		$isbnList = preg_split("/\\r\\n/", $this->_getParam('search'));
				$isbn = implode(',', $isbnList);
				$isbnList = explode(',', $isbn);
				
				$amazon = new Amazon_Service($this->apiKey, $this->countryCode, $this->secretKey);
				
				//The above block of code produces some empty elements, the following block of code checks an ISBNs validity 
				foreach ($isbnList as $isbn)
				{
					$isbnCheck = new Zend_Validate_ISBN();
					if($isbnCheck->isValid($isbn))
					{
						
	    				$item = $amazon->itemLookup($isbn);
	    				var_dump($item->SmallImage->Url->getUri());
	    				//$this->view->item = $item;	
					}
				}
        		//$books = new Model_Books();
        		//$book->addISBNList($this->_getParam('search'));
        	}
        }
        
    	
 		//$amazon = new Amazon_Service($this->apiKey, $this->countryCode, $this->secretKey);
	    //$item =   $amazon->itemLookup('0471684961');
	    //var_dump($item);
	    //$this->view->item = $item;
    }

    public function addAction()
    {
        
    }
    
    public function orderAction(){
    	try{
    		$order = new Model_Orders();
    		$result = $order->getOrderByStatus("2");
    		var_dump($result);
    	}
    	catch (Exception $e)
    	{
    		var_dump($e->getMessage());
    	}
    	
    }


}